var t = require;
var e = module;
var o = exports;
Object.defineProperty(o, "__esModule", {value: !0});
var n = t("BootConst"),
    r = t("WX"),
    a = t("ConfS"),
    i = t("Platforms"),
    s = t("TableRules"),
    c = (function () {
        function t(t, e, o) {
            var n = this;
            i.default.disorder(o),
                o.length > a.DatasManager.横幅缓存数量 && (o.length = a.DatasManager.横幅缓存数量),
                console.log("横幅 " + o.length + " " + o),
                (this.adCtrls = new Array()),
                (this.adIndex = -1),
                (this.检查出错AdIndex = -1),
                (this.addAd = function () {
                    n.adCtrls.length < o.length &&
                        ((n.adId = o[n.adCtrls.length]),
                        console.log("横幅 addAd " + n.adCtrls.length + " " + n.adId),
                        n.adCtrls.push(new l(t, e, n.adId)));
                }),
                this.addAd();
        }
        return (
            (t.prototype.可显示 = function () {
                for (var t = 0, e = this.adCtrls; t < e.length; t++) {
                    if (e[t].可显示()) return !0;
                }
                return !1;
            }),
            (t.prototype.show = function (t, e) {
                this.currAdCtrl && (this.currAdCtrl.hide(), (this.currAdCtrl = null));
                for (var o = 0; o < this.adCtrls.length; o++) {
                    if (
                        (++this.检查出错AdIndex >= this.adCtrls.length && (this.检查出错AdIndex = 0),
                        (n = this.adCtrls[this.检查出错AdIndex]).可显示())
                    );
                    else if ((n.加载中() && n.检查超时(), n.加载中()));
                    else if (n.检查广告出错再次尝试时间()) {
                        n.delayCreate();
                        break;
                    }
                }
                if (((this.rule = t), this.rule.误触))
                    for (o = 0; o < this.adCtrls.length; o++) {
                        if (
                            (++this.adIndex >= this.adCtrls.length && (this.adIndex = 0),
                            (n = this.adCtrls[this.adIndex]).已渲染)
                        )
                            return (
                                (this.currAdCtrl = n),
                                (this.adId = this.currAdCtrl.adId),
                                console.log("横幅 已渲染 adIndex=" + this.adIndex),
                                this.currAdCtrl.show(t),
                                void (e && e(!0))
                            );
                    }
                for (o = 0; o < this.adCtrls.length; o++) {
                    var n;
                    if (
                        (++this.adIndex >= this.adCtrls.length && (this.adIndex = 0),
                        (n = this.adCtrls[this.adIndex]).可显示())
                    )
                        return (
                            (this.currAdCtrl = n),
                            (this.adId = this.currAdCtrl.adId),
                            console.log("横幅 可显示 adIndex=" + this.adIndex),
                            this.currAdCtrl.show(t),
                            void (e && e(!0))
                        );
                }
                e && e(!1);
            }),
            (t.prototype.hide = function () {
                this.rule &&
                    ((this.rule = null),
                    this.currAdCtrl && (this.currAdCtrl.hide(), (this.currAdCtrl = null)),
                    this.addAd());
            }),
            t
        );
    })();
o.default = c;
var l = (function () {
    function t(t, e, o) {
        var n = this;
        (this._onLoaded = function () {
            n.loaded();
        }),
            (this._onResize = function (t) {
                n.resize("onResize", t.width, t.height);
            }),
            (this._onError = function (t) {
                n.log(JSON.stringify(t), !0), n.error();
            }),
            (this.同一横幅展示次数Index = -1),
            (this.同一横幅展示时间Index = -1),
            (this.name = t),
            (this._create = e),
            (this.adId = o),
            this.create();
    }
    return (
        (t.prototype.检查超时 = function () {
            new Date().getTime() - this.开始加载时间 > 1e3 * a.DatasManager.广告加载超时时间 && (this.status = "出错");
        }),
        (t.prototype.检查广告出错再次尝试时间 = function () {
            return (
                !(new Date().getTime() - this.开始加载时间 < 1e3 * a.DatasManager.广告出错再次尝试时间) ||
                (console.error("距离上次拉取时间不足 " + a.DatasManager.广告出错再次尝试时间 + "秒"), !1)
            );
        }),
        (t.prototype.加载中 = function () {
            return "出错" != this.status && ("加载中" == this.status || !(this.wid > 0 && this.hei > 0));
        }),
        (t.prototype.可显示 = function () {
            return this.ad && "加载成功" == this.status && this.wid > 0 && this.hei > 0;
        }),
        (t.prototype.clear = function () {
            this.ad &&
                (this.ad.offLoad && this.ad.offLoad(this._onLoaded),
                this.ad.offResize && this.ad.offResize(this._onResize),
                this.ad.offError && this.ad.offError(this._onError),
                this.ad.destroy && this.ad.destroy(),
                (this.ad = null));
        }),
        (t.prototype.delayCreate = function () {
            var t = this;
            this.clear(),
                i.default.delays.delay({
                    time: s.TableRules.delayCreateTime,
                    action: function () {
                        t.create();
                    }
                });
        }),
        (t.prototype.create = function () {
            this.clear(),
                (this.status = "出错"),
                (this.已渲染 = !1),
                (this.ad = this._create(this.adId, this.rule)),
                this.ad
                    ? ((this.wid = this.hei = 0),
                      (this.剩余展示次数 = this._同一横幅展示次数 = this.同一横幅展示次数()),
                      (this._同一横幅展示时间 = this.同一横幅展示时间()),
                      (this.开始加载时间 = new Date().getTime()),
                      this.ad.onLoad && this.ad.onLoad(this._onLoaded),
                      this.ad.onResize && this.ad.onResize(this._onResize),
                      this.ad.onError && this.ad.onError(this._onError),
                      (this.status = "加载中"),
                      this.log("create"))
                    : this.log("_create失败 " + this.ad, !0);
        }),
        (t.prototype.show = function (t) {
            var e = this;
            if (((this.rule = t), this.ad.style)) {
                var o = i.default.getRect(
                    this.rule,
                    this.wid / i.default.platformCtrl.物理屏幕和画布的比例,
                    this.hei / i.default.platformCtrl.物理屏幕和画布的比例
                );
                (this.ad.style.left = o[0]), (this.ad.style.top = o[1]);
            }
            var a = this.ad.show(r.default.getAdShowArgs(n.dd.banner && n.dd.banner.bannerShow, this.adId));
            a &&
                a.then &&
                a
                    .then(
                        function () {
                            e.已渲染 = !0;
                        },
                        function (t) {
                            e._onError(t);
                        }
                    )
                    .catch(function (t) {
                        e._onError(t);
                    });
        }),
        (t.prototype.hide = function () {
            this.ad.hide(),
                this.rule.误触
                    ? (console.log("强制销毁"), this.delayCreate())
                    : this.展示次数或展示时间达到预定值() && this.delayCreate(),
                (this.rule = null);
        }),
        (t.prototype.loaded = function () {
            this.status = "加载成功";
        }),
        (t.prototype.resize = function (t, e, o) {
            (this.wid = e), (this.hei = o), console.log(this.name + " " + t + " " + this.wid + "x" + this.hei);
        }),
        (t.prototype.error = function () {
            this.status = "出错";
        }),
        (t.prototype.同一横幅展示次数 = function () {
            return a.DatasManager.同一横幅展示次数们[
                this.同一横幅展示次数Index < a.DatasManager.同一横幅展示次数们.length - 1
                    ? ++this.同一横幅展示次数Index
                    : a.DatasManager.同一横幅展示次数们.length - 1
            ];
        }),
        (t.prototype.同一横幅展示时间 = function () {
            return a.DatasManager.同一横幅展示时间们[
                this.同一横幅展示时间Index < a.DatasManager.同一横幅展示时间们.length - 1
                    ? ++this.同一横幅展示时间Index
                    : a.DatasManager.同一横幅展示时间们.length - 1
            ];
        }),
        (t.prototype.展示次数或展示时间达到预定值 = function () {
            if (this.剩余展示次数 > 0 && --this.剩余展示次数 <= 0)
                return (
                    this.log(
                        "展示次数（" +
                            (this._同一横幅展示次数 - this.剩余展示次数) +
                            "）达到预定值（" +
                            this._同一横幅展示次数 +
                            "），重新拉取"
                    ),
                    !0
                );
            if (this.开始加载时间 > 0) {
                var t = (new Date().getTime() - this.开始加载时间) / 1e3;
                if (t > this._同一横幅展示时间)
                    return (
                        this.log("展示时间（" + t + "秒）达到预定值（" + this._同一横幅展示时间 + "），重新拉取"), !0
                    );
            }
            return !1;
        }),
        (t.prototype.log = function (t, e) {
            (e ? console.error : console.log)(this.name + "（" + this.status + "）" + t + " " + this.adId);
        }),
        t
    );
})();
